import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/screenutil.dart';
import 'package:date_format/date_format.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:shop/http/HttpUtil.dart';

class JingyingFenxi extends StatefulWidget {
  JingyingFenxi({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _JingyingFenxi createState() => _JingyingFenxi();
}

class _JingyingFenxi extends State<JingyingFenxi>with SingleTickerProviderStateMixin {
  DateTime startTime=DateTime.now();    //起始时间
  DateTime endTime=DateTime.now();      //结束时间
  String merchantID=""; //商户ID
  int orderNumber=0;    //有效订单数
  int orderNumber2=0;   //失效订单数
  num money=0.00;       //有效营业额
  num money2=0.00;      //失效营业额
  @override
  void initState() {
    _getdata();
  }

  _getdata() async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    merchantID = prefs.getString("merchantID");
    String time1=formatDate(startTime, [yyyy, '-', mm, '-', dd]).toString();
    String time2=formatDate(endTime, [yyyy, '-', mm, '-', dd]).toString();
    var res  =await HttpUtil().get("/waimai/mOder/getDataFenxi?mId="+merchantID+"&startTime="+time1+"&endTime="+time2);
    print(res['data']);
    if(res['code']==200){
      orderNumber=res['data']['orderNumber'];
      orderNumber2=res['data']['orderNumber2'];
      money=res['data']['money'];
      money2=res['data']['money2'];
    }
    if(mounted){
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context , width: 464 , height: 820) ;
    return Scaffold(
      appBar: AppBar(
        title: Text("经营数据"),
      ),
      body:  Stack(
          children:[
            Positioned(
              left: 0,
                right: 0,
                child: Container(
                  padding: EdgeInsets.symmetric(vertical: 20),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      GestureDetector(
                        child: Row(children: [Text(formatDate(startTime, [yyyy, '-', mm, '-', dd]).toString()),Icon(Icons.arrow_drop_down)],),
                        onTap: () async {
                          var result = await showDatePicker(
                          context: context,
                          initialDate: startTime,
                          firstDate: DateTime(2020,1),
                          lastDate: DateTime.now()
                          ).then((DateTime val) async {
                            if(val!=null){
                              this.startTime=val;
                            }
                            setState(() {

                            });
                            _getdata();
                          }).catchError((err) {
                            print(err);
                          });
                        },
                      ),
                      Text("至"),
                      GestureDetector(
                        child: Row(children: [Text(formatDate(endTime, [yyyy, '-', mm, '-', dd]).toString()),Icon(Icons.arrow_drop_down)],),
                        onTap: () async {
                          var result = await showDatePicker(
                              context: context,
                              initialDate: DateTime.now(),
                              firstDate: DateTime(2020,1),
                              lastDate: DateTime.now(),
                              selectableDayPredicate: (DateTime date) {
                                return startTime.difference(date).inDays<=0;
                              },
                          ).then((DateTime val) async {
                            print(66);
                            print(val);
                            if(val!=null){
                              this.endTime=val;
                            }
                            setState(() {

                            });
                            _getdata();
                          }).catchError((err) {
                            print(err);
                          });
                        },
                      )
                    ],
                  ),
                ),
            ),
            Positioned(
                top: 110,
                right: 0,
                left: 0,
                child: Container(
                  padding: EdgeInsets.symmetric(vertical: 20),
                  color: Colors.black12,
                )
            ),
            Positioned(
              top: 50,
                left: 0,
                right: 0,
                child: Container(
                  decoration: BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(10),),color: Colors.orangeAccent	),
                  padding: EdgeInsets.symmetric(vertical: 20),
                  margin: EdgeInsets.symmetric(horizontal: 20),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      Column(
                        children: [
                          Text(orderNumber.toString(),style: TextStyle(color: Colors.white,fontSize: 20),),
                          SizedBox(height: 10,),
                          Text("成功订单(单)",style: TextStyle(color: Colors.white))
                        ],
                      ),
                      Column(
                        children: [
                          Text(money.toString(),style: TextStyle(color: Colors.white,fontSize: 20),),
                          SizedBox(height: 10,),
                          Text("营业额(元)",style: TextStyle(color: Colors.white))
                        ],
                      ),
                    ],
                  ),
                )
            ),
          Positioned(
              top: 150,
              right: 0 ,
              left: 0,
              child: Container(
                padding: EdgeInsets.symmetric(vertical: 10),
                color: Colors.white,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                Column(
                  children: [
                    Text(orderNumber2.toString(),style: TextStyle(fontSize: 20,color: Colors.red),),
                    SizedBox(height: 5,),
                    Text("失效订单(单)",style: TextStyle(color: Colors.red),)
                  ],
                ),
                Column(
                  children: [
                    Text(money2.toString(),style: TextStyle(fontSize: 20,color: Colors.red),),
                    SizedBox(height: 5,),
                    Text("失效金额(单)",style: TextStyle(color: Colors.red),)
                  ],
                ),
              ],
            ),
          )
          ),
            Positioned(child: Container(color: Colors.black12,),top: 220,right: 0,left: 0,bottom: 0,)
        ],
      ),
      backgroundColor:Colors.white
    );
  }

}